package com.carefree.system.modules.admin.controller;

import com.carefree.common.core.PageUtils;
import com.carefree.common.core.Result;
import com.carefree.common.emus.BusinessTypeEnum;
import com.carefree.common.emus.OperatorTypeEnum;
import com.carefree.common.valid.AddGroup;
import com.carefree.common.valid.UpdateGroup;
import com.carefree.system.common.annotation.SystemLog;
import com.carefree.system.modules.admin.entity.RoleEntity;
import com.carefree.system.modules.admin.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 角色
 *
 * @author tengbin
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Resource
    private RoleService roleService;


    /**
     * 列表
     *
     * @param params 分页参数
     * @return
     */
    @SystemLog(value = "分页查询角色信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "分页查询角色信息")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('btn:role:list')")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
        PageUtils page = roleService.queryPage(params);
        return Result.success(page);
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id查询角色信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id查询角色信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('btn:role:list')")
    public Result<RoleEntity> info(@PathVariable("id") Long id) {
        RoleEntity role = roleService.getById(id);
        return Result.success(role);
    }

    /**
     * 保存
     *
     * @param Role 角色
     * @return
     */
    @SystemLog(value = "保存角色信息", businessType = BusinessTypeEnum.INSERT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "保存角色信息")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('btn:role:add')")
    public Result<Object> save(@Validated({AddGroup.class}) @RequestBody RoleEntity role) {
        return roleService.saveRoleData(role) ? Result.ok() : Result.error();
    }

    /**
     * 修改
     *
     * @param Role 角色
     * @return
     */
    @SystemLog(value = "修改角色信息", businessType = BusinessTypeEnum.UPDATE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "修改角色信息")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('btn:role:update')")
    public Result<Object> update(@Validated({UpdateGroup.class}) @RequestBody RoleEntity role) {
        return roleService.updateById(role) ? Result.ok() : Result.error();
    }

    /**
     * 启用或禁用
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id启用或禁用角色", businessType = BusinessTypeEnum.STATUS, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id启用或禁用角色")
    @GetMapping("/active/{id}/{status}")
    @PreAuthorize("hasAuthority('btn:role:enable')")
    public Result<Object> activeOrInActiveById(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return roleService.activeOrInActiveById(id, status) ? Result.ok() : Result.error();
    }


    /**
     * 删除（根据ID 批量删除）
     *
     * @param ids
     * @return
     */
    @SystemLog(value = "根据id批量删除角色信息", businessType = BusinessTypeEnum.DELETE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id批量删除角色信息")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('btn:role:remove')")
    public Result<Object> delete(@RequestBody Long[] ids) {
        return roleService.removeByIds(Arrays.asList(ids)) ? Result.ok() : Result.error();
    }

    /**
     * 根据用户id查询所有角色列表及已分配角色信息
     *
     * @param userId 用户id
     * @return
     */
    @SystemLog(value = "根据用户id查询所有角色列表及已分配角色信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据用户id查询所有角色列表及已分配角色信息")
    @GetMapping("/assign/{userId}")
    @PreAuthorize("hasAuthority('btn:role:list')")
    public Result<Map<String, Object>> toAssignRoles(@PathVariable("userId") Long userId) {
        Map<String, Object> rolesMap = roleService.queryRolesByUserId(userId);
        return Result.success(rolesMap);
    }

}
